package com.feng.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.util.StringUtils;

import java.io.File;
import java.io.IOException;

@Controller
public class FormController {
    @GetMapping("/form_layouts")
    public String form_layouts(){
        return "form/form_layouts";
    }

    @PostMapping("/upload")
    public String upload(@RequestParam("email")String email,
                         @RequestParam("password")String password,
                         @RequestParam("headerImg")MultipartFile headerImg,
                         @RequestParam("photos")MultipartFile[] photos) throws IOException {
        System.out.println(email+"\t"+password);
        System.out.println(headerImg.getSize());
        System.out.println(photos.length);

        if (!headerImg.isEmpty()){
            String originalFilename = headerImg.getOriginalFilename();
            headerImg.transferTo(new File("E:/smallTools/idea/资料/upload/"+originalFilename));
        }
        if (photos.length > 0) {
            for (MultipartFile photo: photos) {
                if (!photo.isEmpty()){
                    String originalFilename = photo.getOriginalFilename();
                    photo.transferTo(new File("E:/smallTools/idea/资料/upload/"+originalFilename));
                }
            }
        }
        System.out.println("数学异常/zero");
        int a = 2/0;
        System.out.println("数学异常/zero2");
        return "index";
    }
}
